//-----------------------------------------------------------------------
// <copyright file="SmartTag.EventReceiver.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation.
// All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
// </copyright>
// <author>Medha</author>
// <date>16-May-2011</date>
//-----------------------------------------------------------------------

namespace SmartTag.Features.SmartTag
{
    using System;
    using System.Runtime.InteropServices;
    using Microsoft.Practices.SharePoint.Common.Configuration;
    using Microsoft.Practices.SharePoint.Common.Logging;
    using Microsoft.Practices.SharePoint.Common.ServiceLocation;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;

    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>
    [Guid("f660223f-bcdd-41da-a805-8c0cd46de799")]
    public class SmartTagEventReceiver : SPFeatureReceiver
    {
        /// <summary>
        /// List of Diagnostics Areas
        /// </summary>
        private static DiagnosticsAreaCollection smartTagArea = null;

        /// <summary>
        /// Gets the SmartTag areas.
        /// </summary>
        public static DiagnosticsAreaCollection SmartTagArea
        {
            get
            {
                if (smartTagArea == null)
                {
                    smartTagArea = new DiagnosticsAreaCollection();
                    DiagnosticsArea newArea = new DiagnosticsArea("SmartTag");
                    newArea.DiagnosticsCategories.Add(new DiagnosticsCategory("Error", EventSeverity.Error, TraceSeverity.Unexpected));
                    smartTagArea.Add(newArea);
                }

                return smartTagArea;
            }
        }

        /// <summary>
        /// Occurs after a Feature is activated.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"/> object that represents the properties of the event.</param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            IConfigManager configMgr = SharePointServiceLocator.GetCurrent().GetInstance<IConfigManager>();

            DiagnosticsAreaCollection configuredAreas = new DiagnosticsAreaCollection(configMgr);

            foreach (DiagnosticsArea newArea in SmartTagArea)
            {
                var existingArea = configuredAreas[newArea.Name];

                if (existingArea == null)
                {
                    configuredAreas.Add(newArea);
                }
                else
                {
                    foreach (DiagnosticsCategory c in newArea.DiagnosticsCategories)
                    {
                        var existingCategory = existingArea.DiagnosticsCategories[c.Name];
                        if (existingCategory == null)
                        {
                            existingArea.DiagnosticsCategories.Add(c);
                        }
                    }
                }
            }

            configuredAreas.SaveConfiguration();
        }

        /// <summary>
        /// Occurs when a Feature is deactivated.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"/> object that represents the properties of the event.</param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            IConfigManager configMgr = SharePointServiceLocator.GetCurrent().GetInstance<IConfigManager>();
            DiagnosticsAreaCollection configuredAreas = new DiagnosticsAreaCollection(configMgr);

            foreach (DiagnosticsArea area in SmartTagArea)
            {
                DiagnosticsArea areaToRemove = configuredAreas[area.Name];

                if (areaToRemove != null)
                {
                    foreach (DiagnosticsCategory c in area.DiagnosticsCategories)
                    {
                        var existingCat = areaToRemove.DiagnosticsCategories[c.Name];
                        if (existingCat != null)
                        {
                            areaToRemove.DiagnosticsCategories.Remove(existingCat);
                        }
                    }

                    if (areaToRemove.DiagnosticsCategories.Count == 0)
                    {
                        configuredAreas.Remove(areaToRemove);
                    }
                }
            }

            configuredAreas.SaveConfiguration();
        }
    }
}